package Design;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
import java.time.LocalDateTime;
import java.util.*;
public class MainFrame extends JFrame {
    private JTable table;
    private DefaultTableModel model;
    private JTextField originTextField,terminusTextField;
    private JButton addButton, deleteButton, searchButton,updateButton ;
    // 主界面
    public MainFrame() {
        init();
        initExcel();
        setLayout(null);
        initTitle();
    }

    private void init() {
        setTitle("客运专线管理系统");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(null);
    }

    private void initTitle() {
        JPanel buttonPanel = new JPanel();
        JLabel originLabel = new JLabel("起始站:");
        originTextField = new JTextField(5);
        JLabel terminusLabel = new JLabel("终点站");
        terminusTextField = new JTextField();


        //增删改查
        addButton = new JButton();
        addButton.setBounds(650,50,100,30);
        addButton.setText("增加");
        this.add(addButton);

        deleteButton = new JButton();
        deleteButton.setBounds(650,120,100,30);
        deleteButton.setText("删除");
        this.add(deleteButton);

        updateButton = new JButton();
        updateButton.setBounds(650,190,100,30);
        updateButton.setText("修改");
        this.add(updateButton);

        searchButton = new JButton();
        searchButton.setBounds(650,260,100,30);
        searchButton.setText("查找");
        this.add(searchButton);

        addButton.addActionListener(e -> addButtonClicked());
        deleteButton.addActionListener(e -> deleteButtonClicked());
        searchButton.addActionListener(e -> searchButtonClicked());
        updateButton .addActionListener(e -> updateButtonClicked());
    }

    // 增加监听接口
    private void addButtonClicked() {
        JFrame addFrame = new JFrame("添加火车信息");
        addFrame.setSize(400, 300);
        addFrame.setLayout(new BorderLayout());
        addFrame.setLocationRelativeTo(null);
        JPanel formPanel = new JPanel(new GridLayout(0, 2));
        // Date picker

        // Text fields for train details
        JTextField idField = new JTextField();
        JTextField originField = new JTextField();
        JTextField terminusField = new JTextField();
        JTextField time1Field = new JTextField();
        JTextField time2Field = new JTextField();
        JTextField priceField = new JTextField();
        formPanel.add(new JLabel("车次:"));
        formPanel.add(idField);
        formPanel.add(new JLabel("起始站:"));
        formPanel.add(originField);
        formPanel.add(new JLabel("终点站:"));
        formPanel.add(terminusField);
        formPanel.add(new JLabel("起始时间:"));
        formPanel.add(time1Field);
        formPanel.add(new JLabel("到达时间:"));
        formPanel.add(time2Field);
        formPanel.add(new JLabel("票价:"));
        formPanel.add(priceField);

        // Dropdown for train type
        String[] trainTypes = {"高客线", "快客线"};
        JComboBox<String> typeDropdown = new JComboBox<>(trainTypes);
        formPanel.add(new JLabel("类型(Type):"));
        formPanel.add(typeDropdown);

        // Add button to submit form
        JButton submitButton = new JButton("添加");
        submitButton.addActionListener(e -> {
            TrainDao trainDao = new TrainDao();
            LocalDateTime date;
            // 这里应该有插入数据的逻辑，例如调用一个方法来从数据库中插入数据
            TrainInfo trainInfo = new TrainInfo();
            trainInfo.setId(idField.getText());
            trainInfo.setOrigin(originField.getText());
            trainInfo.setTerminus(terminusField.getText());
            trainInfo.setTime1(time1Field.getText());
            trainInfo.setTime2(time2Field.getText());
            trainInfo.setPrice(Double.parseDouble(priceField.getText()));
            trainInfo.setType(typeDropdown.getSelectedItem().toString());
            trainInfo.setDate(LocalDateTime.now().toString());

            if(trainDao.insertTrainData(trainInfo))
            {
                //插入成功提示框
                JOptionPane.showMessageDialog(this, "增加成功");
            }
            else{
                //错误提示框
                JOptionPane.showMessageDialog(this, "增加失败");
            }
            initExcel();
            // 示例：insertTrainData(id, origin, terminus, time1, time2, price, type);
        });

        addFrame.add(formPanel, BorderLayout.CENTER);
        addFrame.add(submitButton, BorderLayout.SOUTH);

        addFrame.setVisible(true);
    }
    //  删除监听接口
    private void deleteButtonClicked() {
        TrainDao trainDao = new TrainDao();
        int selectedRow = table.getSelectedRow();
        if (selectedRow != -1) {
            String id = table.getValueAt(selectedRow, 1).toString();
            // 这里应该有删除数据的逻辑，例如调用一个方法来从数据库中删除数据
            trainDao.deleteTrainDataById(id);
            JOptionPane.showMessageDialog(this, "删除成功");
            // 刷新表格数据
            initExcel();
        } else {
            JOptionPane.showMessageDialog(this, "请选择要删除的火车信息");
        }
    }


    // 查询监听接口
    private void searchButtonClicked() {
        String origin = originTextField.getText();
        String terminus = terminusTextField.getText();
        // 调用searchTrainData方
        //TrainDeal.searchTrainData(origin,terminus);
        JOptionPane.showMessageDialog(this, "搜索成功");
        // 刷新表格数据
        initExcel();
    }
    // 修改监听接口
    private void updateButtonClicked() {
        TrainInfo trainInfo = new TrainInfo();
        TrainDao trainDao = new TrainDao();
        int selectedRow = table.getSelectedRow();
        if (selectedRow != -1) {
            String id = table.getValueAt(selectedRow, 1).toString();
            trainInfo = trainDao.searchTrainDataById(id);
            JFrame updateFrame = new JFrame("修改火车信息");
            updateFrame.setSize(400, 300);
            updateFrame.setLayout(new BorderLayout());
            updateFrame.setLocationRelativeTo(null);

            JPanel formPanel = new JPanel(new GridLayout(0, 2));

            JTextField idField = new JTextField();
            JTextField originField = new JTextField();
            JTextField terminusField = new JTextField();
            JTextField time1Field = new JTextField();
            JTextField time2Field = new JTextField();
            JTextField priceField = new JTextField();


            String selectedId = trainInfo.getId(); // Replace with actual selected ID
            String selectedOrigin = trainInfo.getOrigin(); // Replace with actual selected origin
            String selectedTerminus = trainInfo.getTerminus(); // Replace with actual selected terminus
            String selectedTime1 = trainInfo.getTime1(); // Replace with actual selected time1
            String selectedTime2 = trainInfo.getTime2(); // Replace with actual selected time2
            String selectedPrice = trainInfo.getPrice().toString(); // Replace with actual selected price

            idField.setText(selectedId);
            originField.setText(selectedOrigin);
            terminusField.setText(selectedTerminus);
            time1Field.setText(selectedTime1);
            time2Field.setText(selectedTime2);
            priceField.setText(selectedPrice);

            formPanel.add(new JLabel("车次："));
            formPanel.add(idField);
            formPanel.add(new JLabel("起始站："));
            formPanel.add(originField);
            formPanel.add(new JLabel("终点站："));
            formPanel.add(terminusField);
            formPanel.add(new JLabel("起始时间："));
            formPanel.add(time1Field);
            formPanel.add(new JLabel("到达时间："));
            formPanel.add(time2Field);
            formPanel.add(new JLabel("票价："));
            formPanel.add(priceField);

            String[] trainTypes = {"高客线", "快客线"};
            JComboBox<String> typeDropdown = new JComboBox<>(trainTypes);
            formPanel.add(new JLabel("类型(Type):"));
            formPanel.add(typeDropdown);

            JButton submitButton = new JButton("修改");
            submitButton.addActionListener(e -> {
                TrainDao trainDao1 = new TrainDao();
                TrainInfo trainInfo1 = new TrainInfo();
                LocalDateTime date;
                // 这里应该有插入数据的逻辑，例如调用一个方法来从数据库中插入数据
                trainInfo1 = new TrainInfo();
                trainInfo1.setId(idField.getText());
                trainInfo1.setOrigin(originField.getText());
                trainInfo1.setTerminus(terminusField.getText());
                trainInfo1.setTime1(time1Field.getText());
                trainInfo1.setTime2(time2Field.getText());
                trainInfo1.setPrice(Double.parseDouble(priceField.getText()));
                trainInfo1.setType(typeDropdown.getSelectedItem().toString());
                trainInfo1.setDate(LocalDateTime.now().toString());

                trainDao1.updateTrainData(id,trainInfo1);

                //插入成功提示框
                JOptionPane.showMessageDialog(this, "修改成功");
                initExcel();
            });
            updateFrame.add(formPanel, BorderLayout.CENTER);
            updateFrame.add(submitButton, BorderLayout.SOUTH);
            updateFrame.setVisible(true);

        } else {
            JOptionPane.showMessageDialog(this, "请选择要修改的火车信息");
        }

    }
    private void initExcel() {
        // 创建表格模型
        model = new DefaultTableModel();
        model.addColumn("日期");
        model.addColumn("车次");
        model.addColumn("起始站");
        model.addColumn("终点站");
        model.addColumn("起始时间");
        model.addColumn("到达时间");
        model.addColumn("票价");
        model.addColumn("类型");

        // 创建表格并设置模型
        table = new JTable(model);
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setLocation(0,0);
        scrollPane.setSize(600,600);
        add(scrollPane, BorderLayout.CENTER);
        addRowToTableAll();
        setVisible(true);
    }

    private void addRowToTable(String id) {
        TrainDao trainDao = new TrainDao();
        TrainInfo trainInfo = trainDao.searchTrainDataById(id);
        if(trainInfo!= null){
            Vector<String> row = new Vector<>();
            row.add(trainInfo.getDate().toString());
            row.add(trainInfo.getId());
            row.add(trainInfo.getOrigin());
            row.add(trainInfo.getTerminus());
            row.add(trainInfo.getTime1());
            row.add(trainInfo.getTime2());
            row.add(trainInfo.getPrice().toString());
            row.add(trainInfo.getType());
            model.addRow(row);
        }
    }

    //显示所有
    private void addRowToTableAll() {
        TrainDao trainDao = new TrainDao();
        ArrayList<TrainInfo> trainInfos = trainDao.searchTrainDataAll();
        for(int i = 0; i<trainInfos.size();i++){
            Vector<String> row = new Vector<>();
            row.add(trainInfos.get(i).getDate().toString());
            row.add(trainInfos.get(i).getId());
            row.add(trainInfos.get(i).getOrigin());
            row.add(trainInfos.get(i).getTerminus());
            row.add(trainInfos.get(i).getTime1());
            row.add(trainInfos.get(i).getTime2());
            row.add(trainInfos.get(i).getPrice().toString());
            row.add(trainInfos.get(i).getType());
            model.addRow(row);
        }
    }

    // 添加行到表格的方法
//    private void addRowToTable(String date, String id, String origin, String terminus, String time1, String time2, String price, String type) {
//        Vector<String> row = new Vector<>();
//        row.add(date);
//        row.add(id);
//        row.add(origin);
//        row.add(terminus);
//        row.add(time1);
//        row.add(time2);
//        row.add(price);
//        row.add(type);
//        model.addRow(row);
//
//    }
    public static void main(String[] args) {
        // 使用事件调度线程来创建UI
        SwingUtilities.invokeLater(() -> new MainFrame());
    }
}
